/*绑定回车到查询按钮*/
$(function(){
	$(document).keydown(function(){ 
		if(event.keyCode==13){ 
		$("#sflist-chaxun").trigger('click'); 
		return false;
		}
	});
});
//进入收费页面默认重新加载一次该页面（和点击重置一样的效果）
$(function(){
	$("#shoujimc").val('');
	$('#shoufei-kaishjsj').val('');
	$('#shoufei-jieshusj').val('');
	xingmingsj='';
  	kaishisj = '';
	jieshusj = '';
	pageNumber=1;
	dwpanduan=false;
 	showSflb();
});

//传值变量
var xingmingsj = ""; 
var kaishisj="";
var jieshusj="";
// 定位参数
var dwshoufeiid;
var dwpageNo;
var dwkaishisj;
var dwjieshusj;
var dingwei;
var dwxingmingsj;
var pageNumber;
var dwpanduan=true;
function getLiebiaocs(preshoufeiid,prepageNo,prekaishisj,prejieshusj,prexingmingsj){
	if(preshoufeiid!='null'&&preshoufeiid!=null){
		dwshoufeiid=preshoufeiid;
		pageNumber=prepageNo;
		kaishisj=prekaishisj;
		jieshusj=prejieshusj;
		console.log(dwshoufeiid)
		$('#shoufei-kaishjsj').val(kaishisj);
		$('#shoufei-jieshusj').val(jieshusj);
		if(prexingmingsj!=''&&prexingmingsj!='null'){
			xingmingsj=prexingmingsj;
			$("#shoujimc").val(xingmingsj);
		}
	}else{
		pageNumber=1;
		xingmingsj='';
		jieshusj='';
		kaishisj='';
	}
	showSflb();
}
//数据读取
function showSflb() {
    var totalNumber = 0;
	var params={
			'kaishisj':kaishisj,
		    'jieshusj':jieshusj,
		    'xingmingdh':xingmingsj,
		};
	$.ajax({
	    type:"POST",
	    url:"./shoufei/getCount",
	    data:params,
	    async:false,
	    success: function(data){
	    	if(data!=null) {
	    		totalNumber = data;
	    		if(totalNumber==0){
	    			$("#sflist-neirong").css("display","none");
	    			$("#sflist-img").html("<img src= '"+path+"/image/chaxtj/cxtj-nobody.png'>");
	    		}else{
	    			$("#sflist-neirong").css("display","block");
	    			$("#sflist-img").html('');
	    		}
			}
		}
	});
	function createSflb(name) {
		dwxingmingsj=encodeURI(encodeURI($("#shoujimc").val()));
    	dwjieshusj=$('#shoufei-kaishjsj').val();
		dwkaishisj=$('#shoufei-jieshusj').val();
		$('#tbshoufei').html('');
	    var container = $('#pagination-'+name);
	    container.pagination({
	    dataSource:'./shoufei/getShoufeiByPage?kaishisj='+kaishisj+'&jieshusj='+jieshusj+'&xingmingdh='+xingmingsj,
	    locator: 'entities',
	    pageSize: 12,
	    pageNumber:pageNumber,
	    totalNumber: totalNumber,
	    
	    callback: function(data, pagination) {
	    	if(data!='') {
				var content = '';
				$(data).each(function(index,e){
					if(data[index].xingbie==null || data[index].xingbie =="undefined" || data[index].xingbie =="" ){
	                    var sex="";
	                }
	            	if(data[index].xingbie==1){
	               		var sex='\/'+'\ '+"男";
	                }else if(data[index].xingbie==2){
	               	 	var sex='\/'+'\ '+"女";
	                }else{
	                	var sex="";
	                }
	            	if(data[index].nianling==null) {
	            		var nianling = "";
	            	}else{
	            		var nianling='/'+data[index].nianling+data[index].nianlingdw;
	            	}
	            	if(data[index].shoujihm==null) {
	            		var shoujihm = "";
	            	}else{
	            		var shoujihm='/'+data[index].shoujihm;
	            	}
	            	if(data[index].jibingmc==null) {
	            		var zhenduan = "";
	            	}else{
	            		var zhenduan="诊断："+data[index].jibingmc;
	            	}
	            	if(data[index].yewurk==1 ){
	              		var leixings="微诊疗";
	                }else if(data[index].yewurk==2) {
	                	var leixings="售药";
	                }else if(data[index].yewurk==3) {
	                	var leixings="常规门诊";
	                }
	            	  var st1 ="应付款：";
	            	  var st2="元";
	            	 if(zhenduan.length>18){
	            		 zhenduan=zhenduan.substring(0,18);
	            	 }else{
	            		 zhenduan=zhenduan;
	            	 }
	            	 if(dwshoufeiid==this.id){
						dingwei=this.id;
	        		}
	            	var gerenxx = data[index].xingming+'\ '+sex+'\ '+nianling+'\ '+shoujihm+'\ '+'\/'+'\ '+data[index].showtime+'\ '+'\/'+'\ '+leixings+'\ '+zhenduan+'\ '+'\/'+'\ '+st1+parseFloat(data[index].yingshouje).toFixed(2)+st2;
	            	content+="<tr id='shoufei"+data[index].id+"'>";
	            	content=content+"<td class='col-xs-1 col-sm-1'>"+(((pagination.pageNumber-1)*pagination.pageSize)+index+1)+"</td>";
	            	content=content+'<td class="col-xs-8 col-sm-9" style="text-align:left;"><input type="hidden" class="input col-xs-12" style="padding:0;border:0;background-color:#fff" id="shoufei-gerenxx">'+gerenxx+'</td>' ;
	                content=content+'<td style="text-align:center;"><a class="operation"  id="'+data[index].id+'" onclick="toShoufeixq('+data[index].id+','+data[index].yewurk+','+pagination.pageNumber+')">收费</a>';
	                content=content+'<a class="operation"  id="'+data[index].id+'" style="margin-left:1rem"  data-toggle="modal" onclick="shoufei_delete('+data[index].id+',$(this))">作废</a>';
	                content=content+'<a class="operation"  id="'+data[index].id+'" style="margin-left:1rem"  data-toggle="modal" onclick="shoufei_xiugai('+data[index].jiuzhenjlid+','+data[index].id+',\''+data[index].yisheng+'\','+data[index].yewurk+',$(this))">修改</a></td>';
				});
				$("#tbshoufei").html(content);
				if(dingwei!=null&&dingwei!=''){
		 	        $('#tbshoufei').find('tr[id="shoufei'+dingwei+'"]').addClass('list_tr');
		        }
		        if(!dwpanduan){
		        	$('#tbshoufei').find('tr[id="shoufei'+dingwei+'"]').removeClass('list_tr');
		        }
	    	}
	    }
	  });
	  return container;
	}
	createSflb('fenysf');
	if(totalNumber <= 12) {
		$('#pagination-fenysf').css('display','none');
	}else{
		$('#pagination-fenysf').css('display','block');
	}
}

//查询按钮
$("#sflist-chaxun").click(function(){
	 xingmingsj=$("#shoujimc").val();
  	 kaishisj=$('#shoufei-kaishjsj').val();
     jieshusj=$('#shoufei-jieshusj').val();
     pageNumber=1;
	 dwpanduan=false;
 	 showSflb();
});

//优惠价格功能
$(".f-shoyfei-yh").focusout(function(){
	var yh = $(this).val();
	var zj =$(".f-shoyfei-zj").text();
	var yf =zj-yh;
	$(".f-shoyfei-yf").text(yf);
});


//查询条件置空按钮
$("#sflist-tjreset").click(function(){
	$("#shoujimc").val('');
	$('#shoufei-kaishjsj').val('');
	$('#shoufei-jieshusj').val('');
	xingmingsj='';
  	kaishisj = '';
	jieshusj = '';
	pageNumber=1;
	dwpanduan=false;
 	showSflb();
});

//删除记录
var ku_id = null;
function shoufei_delete(id,$this){
	$this.parent().parent().siblings('tr').removeClass('list_tr');
	ku_id = id;
	$("#shoufei-tanceng-delete").modal();
}
$("#shoufei-tanceng-delete .confirm").click(function(){
	
	var id=ku_id;
	var sendData ={
			"id":id,
			"zuofei":4
	}
   	$.ajax({
		 type:"POST",
	        //获取json文件路径
	        url:"./shoufei/zuofeisf",
	        //传送数据
	        data:sendData,
	        //请求成功完成后要执行的方法
	        success: function(data){
   		$("#shoufei-tanceng-delete").modal('hide');
   		dwpanduan=false;
	    showSflb();
	    notifySuccess("作废成功");
	        	    
	        }
	});
    
});
var menzhenqx="";
var shouyaoqx="";
//获取门诊权限
function getMenzhenqx(prechangguimzqx,preweizhenlqx,preshouyaoqx){
	if(preweizhenlqx=='true'&&prechangguimzqx=='true'){
		menzhenqx='true';
	}
	shouyaoqx=preshouyaoqx;
}
//修改就诊记录状态为保存
var ex_jiuzhenjlid = null;
var ex_shoufeijlid = null;
var ex_yisheng=null;
var yewulx=null;
function shoufei_xiugai(jiuzhenjlid,shoufeijlid,yisheng,preyewulx,$this) {
	$this.parent().parent().siblings('tr').removeClass('list_tr');
	ex_jiuzhenjlid = jiuzhenjlid;
	ex_shoufeijlid = shoufeijlid;
	ex_yisheng=yisheng;
	yewulx=preyewulx;
	$("#shoufei-tanceng-xiugai").modal();
}
$("#shoufei-tanceng-xiugai .btnQueding").click(function(){
	$.ajax({
		 type:"POST",
	       //获取json文件路径
	       url:"./shoufei/xiugaijzjl",
	       //传送数据
	       data:{jiuzhenjlid:ex_jiuzhenjlid,shoufeijlid:ex_shoufeijlid},
	       //请求成功完成后要执行的方法
	       success: function(data){
	    	   $("#shoufei-tanceng-xiugai").modal('hide');
	    	   var sjc=new Date().getTime();
	    	   if(yewulx==3||yewulx==1){
	    		   if(menzhenqx=='true'){
		    		   notifySuccess("提交修改成功，请到微诊疗或常规门诊的今日门诊查看该记录",sjc);
	    		   }else{
	    		   		notifySuccess("已提交给"+ex_yisheng+"医生修改",sjc);
	    		   		}
	    		   }
	    	   if(yewulx==2){
	    		   if(shouyaoqx=='true'){
		    		    notifySuccess("提交修改成功，请到售药模块的今日售药查看该记录",sjc);
	    		   }else{
						notifySuccess("已提交给"+ex_yisheng+"医生修改",sjc);
	    	   			}
		       	   }
	    	   	  dwpanduan=false;
		       	  showSflb();
		       	  }
		});
});

$("#shoufei-tanceng-delete cancel").click(function(){
	window.history.back(-1);
});
//进入收费详情页面
function toShoufeixq(id,preyewulx,prepageNo){
	window.location.href=path+"/views/shoufei/shoufei-detail.jsp?ID="+id+"&pageNo="+prepageNo+"&kaishisj="+dwkaishisj+"&jieshusj="+dwjieshusj+"&xingmingsj="+dwxingmingsj;
}
//时间插件
function myDateTimePicker(firstDate,secondDate){
	var dbkaishisj;
	var dbjieshusj;
	$.ajax({
		type:"post",
		url:"./base/getShujukuTime",
		async:false,
		success: function(data){
		dbkaishisj=data.dbMonthFirstDay;
		dbjieshusj=data.dbCurrentDate;
		}
	});
	getShujukuTime(dbjieshusj);
    $(firstDate).datetimepicker({
   	    format:"L",
        allowInputToggle:true,
    	showTodayButton:true,
    });
    $(secondDate).datetimepicker({
   	 	format:"L",
        allowInputToggle:true,
    	showTodayButton:true,
    	});
    $(secondDate).on("dp.change", function (e) {
        $(firstDate).data("DateTimePicker").maxDate(e.date);
    });
    $(firstDate).on("dp.change", function (e) {
        $(secondDate).data("DateTimePicker").minDate(e.date);
    });
}